#!/bin/sh

set -e

NTPDRS_CONF_SRC="/usr/share/doc/ntpd-rs/ntp.toml.default"
NTPDRS_CONF_DIR="/etc/ntpd-rs"
NTPDRS_CONF="${NTPDRS_CONF_DIR}/ntp.toml"
NTPDRS_CONF_PERMS=644
NTPDRS_HOME="/var/lib/ntpd-rs/"
NTPDRS_USER="ntpd-rs"
NTPDRS_OBSERVE_HOME="/var/lib/ntpd-rs-observe/"
NTPDRS_OBSERVE_USER="ntpd-rs-observe"

create_user() {
    if id ${NTPDRS_USER} > /dev/null 2>&1; then return; fi
    adduser --system --home "${NTPDRS_HOME}" --group ${NTPDRS_USER}
    adduser --system --home "${NTPDRS_OBSERVE_HOME}" --group ${NTPDRS_OBSERVE_USER}
}

create_first_time_configuration() {
    if [ ! -f "${NTPDRS_CONF}" ]; then
        mkdir -p "${NTPDRS_CONF_DIR}"
        cp "${NTPDRS_CONF_SRC}" "${NTPDRS_CONF}"
        chown "${NTPDRS_USER}:${NTPDRS_USER}" "${NTPDRS_CONF_DIR}"
        chown "${NTPDRS_USER}:${NTPDRS_USER}" "${NTPDRS_CONF}"
        chmod "${NTPDRS_CONF_PERMS}" "${NTPDRS_CONF}"
    fi
}

case "$1" in
configure)
    create_user
    create_first_time_configuration
    ;;
esac


#DEBHELPER#
